import api from '../../api';
import { message } from 'antd';
import { useCallback } from 'react';
export default {
  namespace: 'initialSplitM',

  state: {
    productList: [],
  },

  effects: {
    //删除系列产品Id
    *delBySeriesProudctId({ payload, callback }, { call, put }) {
      const response = yield call(api.initialSplit.delBySeriesProudctId, payload);
      callback(response);
    },
    //智能分配
    *smartAssign({ payload, callback }, { call, put }) {
      const response = yield call(api.initialSplit.smartAssign, payload);
      callback(response);
    },
    //拆分确认
    *splitConfirm({ payload, callback }, { call, put }) {
      const response = yield call(api.initialSplit.splitConfirm, payload);
      callback(response);
    },
    //查看产品信息
    *queryProductList({ payload, callback }, { call, put }) {
      const response = yield call(api.initialSplit.queryProductList, payload);
      if (response) {
        if (response.code == 0) {
          callback('success', response.data);
          yield put({
            type: 'save',
            payload: {
              productList: response.data.dataSource,
            },
          });
        } else {
          callback('fail');
          message.error(response.msg || '查询产品信息列表失败!');
        }
      } else {
        callback('fail');
        message.error('查询产品信息列表失败!');
      }
    },
    //查看未分配资产列表
    *queryUnAssignAsset({ payload, callback }, { call, put }) {
      const response = yield call(api.initialSplit.queryUnAssignAsset, payload);

      if (response) {
        if (response.code == 0) {
          callback('success', response.data);
        } else {
          callback('fail');
          message.error(response.msg || '查询未分配资产失败!');
        }
      } else {
        callback('fail');
        message.error('查询未分配资产失败!');
      }
    },

    //查看已分配资产列表
    *queryAssignedAsset({ payload, callback }, { call, put }) {
      const response = yield call(api.initialSplit.queryAssignedAsset, payload);
      if (response) {
        if (response.code == 0) {
          callback('success', response.data);
        } else {
          callback('fail');
          message.error(response.msg || '查询已分配资产失败!');
        }
      } else {
        callback('fail');
        message.error('查询已分配资产失败!');
      }
    },
    //点击试算
    //
    *tryCalc({ payload, callback }, { call, put }) {
      const response = yield call(api.initialSplit.tryCalc, payload);
      if (response) {
        if (response.code !== null || response.code !== undefined) {
          //后端返回code 并非报错 只要按照信息展示即可
          callback('success', response.data || {});
          if (response.code == 1) message.info(response.msg);
        } else {
          callback('fail');
          message.error(response.msg || '试算失败!');
        }
      } else {
        callback('fail');
        message.error('试算失败!');
      }
    },
    //点击确定
    *confirmCalc({ payload, callback }, { call, put }) {
      const response = yield call(api.initialSplit.confirmCalc, payload);
      if (response) {
        if (response.code == 0) {
          callback('success', response.data);
        } else {
          callback('fail');
          message.error(response.msg || '手工调整失败!');
        }
      } else {
        callback('fail');
        message.error('手工调整失败!');
      }
    },
    //点击确定 新增系列产品
    *uploadFile({ payload, callback }, { call, put }) {
      const response = yield call(api.initialSplit.uploadFile, payload);
      callback(response);
    },
    // 系列产品代码查重
    *checkListCode({ payload, callback }, { call, put }) {
      const response = yield call(api.initialSplit.checkListCode, payload);
      callback(response);
    },
    //拆分配置保存
    *saveConfig({ payload, callback }, { call, put }) {
      const response = yield call(api.initialSplit.saveConfig, payload);
      callback(response);
    },
    //拆分配置查询
    *getConfig({ payload, callback }, { call, put }) {
      const response = yield call(api.initialSplit.getConfig, payload);
      callback(response);
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
